package com.kidgestures.activities;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.kidsgestures.R;
import com.kidgestures.utils.Randomizer;

import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnGesturePerformedListener {
	private GestureLibrary gLibrary;
	private int random;
	private HashMap <Integer, String> letters = new HashMap <Integer,String>();
	TextView txtLetter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
	
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initLetters(this.letters);
		gLibrary = 
	              GestureLibraries.fromRawResource(this, R.raw.gestures);
	            if (!gLibrary.load()) {
	               finish();
	            }   
	            
	         GestureOverlayView gOverlay = 
	                    (GestureOverlayView) findViewById(R.id.gOverlay);
	          gOverlay.addOnGesturePerformedListener(this);
	          txtLetter= (TextView) findViewById(R.id.textTarget);
	          this.random=Randomizer.generateItem(0);
	          txtLetter.setText(getLetter(this.random));
	          
	}
	
	
	private void initLetters(HashMap<Integer, String> lettersMap) {

     lettersMap.put(1, "a");
     lettersMap.put(2, "e");
     lettersMap.put(3, "i");
     lettersMap.put(4, "o");
     lettersMap.put(5, "u");
     
		
	}


	private CharSequence getLetter(int randomNumber) {
		// TODO Auto-generated method stub
		return this.letters.get(Integer.valueOf(randomNumber));
	}


	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
        
        if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
            
        	String action = predictions.get(0).name;
        	if(action.equals(getLetter(this.random))){
        		Toast.makeText(this, "YES", Toast.LENGTH_SHORT).show();
        	} else{
        		Toast.makeText(this, "NO", Toast.LENGTH_SHORT).show();
        	}
        	
        	this.random=Randomizer.generateItem(0);
        	txtLetter.setText(getLetter(this.random));
            
        }
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
